package com.wx.service.impl;

// service：逻辑垃圾箱，用于实现新功能

import com.wx.dto.AdminRegDTO;
import com.wx.dto.AdminUpdateDTO;
import com.wx.dto.BookTypeDTO;
import com.wx.dto.BorrowAgreeDTO;
import com.wx.entity.Admin;
import com.wx.entity.Borrow;
import com.wx.entity.Notice;
import com.wx.entity.User;
import com.wx.exception.BaseException;
import com.wx.mapper.*;
import com.wx.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BorrowMapper borrowMapper;

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public Admin login(Admin admin) {
        Admin byUsername = adminMapper.getByUsername(admin.getUsername());
        if(byUsername == null){
            throw new BaseException("管理员账号不存在");
        }
        if(!byUsername.getPassword().equals(admin.getPassword())){
            throw new BaseException("密码错误");
        }
        if(byUsername.getIsFrozen() == 1){
            throw new BaseException("账号被停用");
        }
        byUsername.setPassword("******");
        return byUsername;
    }

    /*管理员注册*/
    @Override
    public void save(AdminRegDTO adminRegDTO) {
        // 验证管理员用户名不重复
        Admin admin1 = adminMapper.getByUsername(adminRegDTO.getUsername());
        if(admin1 != null){
            throw new BaseException("用户名已存在!");
        }

        //创建实体类
        Admin admin = new Admin();

        //复制实体类相同属性
        BeanUtils.copyProperties(adminRegDTO, admin);

        //补全属性
        admin.setIsFrozen(0);
        admin.setUpdateUser(adminRegDTO.getUsername());
        admin.setCreateTime(LocalDateTime.now());
        adminMapper.save(admin);

        //todo:Md5加密
    }

    /*获取管理员列表*/
    @Override
    public List<Admin> getList(){

        List<Admin> adminList = adminMapper.getAdminList();
        return adminList;
    }

    /*更新管理员信息*/
    @Override
    public void updateAdmin(AdminUpdateDTO adminUpdateDTO) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminUpdateDTO, admin);
        adminMapper.update(admin);
    }

    /*同意/拒绝 借书请求*/
    @Override
    public void agreeBorrow(BorrowAgreeDTO borrowAgreeDTO) {

        borrowMapper.setIsAgree(borrowAgreeDTO);

    }

    /*同意还书*/
    @Override
    public void confirmreturn(BorrowAgreeDTO borrowAgreeDTO) {
        borrowMapper.setIsReturn(borrowAgreeDTO);
    }

    @Override
    public void setnotice(Notice notice) {
        /*查询username是否存在*/
        User byUsername = userMapper.getByUsername(notice.getUsername());
        if(byUsername == null){
            throw new BaseException("用户账号不存在");
        }
        notice.setUpdateTime(LocalDateTime.now());
        notice.setIsConfirm(0);
        noticeMapper.setnotice(notice);
    }
}
